﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Common;
using System.Drawing;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;

public partial class Views_TZoneKind : System.Web.UI.Page
{
    int lbl_LaneNo = 1; //巷道号
    int iLcol = 0;

    DataSet ds = null;
    string strColor = "";
    string strUserNo = "";     //用户编号
    int PosState = 0;           //货位状态
    int iLineNo = 0;               //排
    int iLayNo = 0;           //层
    string PID = "";
    Nova_BLL.TZoneDefine Nova_BLL_TZoneDefine = new Nova_BLL.TZoneDefine();
    Nova_BLL.TLaneDefine Nova_BLL_TLaneDefine = new Nova_BLL.TLaneDefine();
    Nova_BLL.TBase_Position Nova_BLL_TBase_Position = new Nova_BLL.TBase_Position();
    Nova_Common.LoginLogic cLoginLogic = new LoginLogic();
    Nova_BLL.TPosState Nova_BLL_TPostState = new Nova_BLL.TPosState();
    Nova_MOD.TBase_Position Nova_MOD_TBase_Position = new Nova_MOD.TBase_Position();

    protected void Page_Load(object sender, EventArgs e)
    {
        GV_TZDefine_bind();
        if (!IsPostBack)
        {
            Nova_Common.LoginLogic.IsLogin();
            GV_TZoneDefineStyle();
            ddlbind();
            GV_TZoneDefine_bind();
            DL_TZDefine_bind();
        }
    }

    protected void GV_TZoneDefine_bind()         //库区分类视图表
    {
        lbl_LaneNo = Convert.ToInt32(ddlNo_Lane.SelectedValue.ToString());
        ds = Nova_BLL_TZoneDefine.DataListTZoneDefine(lbl_LaneNo);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            GV_TZoneDefine.DataSource = ds;
            GV_TZoneDefine.DataBind();
        }
    }

    protected void GV_TZoneDefine_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string cid = e.Row.Cells[1].Text.ToString();
            int rid = e.Row.RowIndex + 1;
            for (int i = 2; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;

                strColor = Nova_BLL_TZoneDefine.getColor(e.Row.Cells[i].Text.ToString());
                strColor = "#" + strColor;
                if (strColor != null && strColor != "")
                {
                    e.Row.Cells[i].BackColor = System.Drawing.Color.FromName(strColor);
                }
                e.Row.Cells[i].Text = "";
                e.Row.Cells[i].Attributes["onclick"] = "showEdit(" + lbl_LaneNo + "," + e.Row.Cells[0].Text.ToString() + "," + i + "," + cid + "," + rid + ");";
            }
        }
    }

    //巷道绑定
    protected void ddlbind()
    {
        ds = Nova_BLL_TLaneDefine.DataSetTLaneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane.DataSource = ds;
            ddlNo_Lane.DataValueField = "No_Lane";
            ddlNo_Lane.DataTextField = "No_Lane_Text";
            ddlNo_Lane.DataBind();
        }
    }

    //绑定宽度和高度
    protected void GV_TZoneDefineStyle()         //绑定宽度和高度
    {

        string strTableNameTZoneDefine = "TZoneDefine";
        ds = Nova_BLL_TBase_Position.getGVStyle(strTableNameTZoneDefine, strUserNo);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            GV_TZoneDefine.Width = Unit.Parse(ds.Tables[0].Rows[0]["Width"].ToString());
            GV_TZoneDefine.Height = Unit.Parse(ds.Tables[0].Rows[0]["Height"].ToString());
        }
        else
        {
            GV_TZoneDefine.Width = Unit.Parse("100%");
        }
    }

    protected void ibtnTZoneDefine_OK_Click(object sender, ImageClickEventArgs e)
    {
        strUserNo = cLoginLogic.BackNovaUserNo();
        if (txtTZoneWidth.Text == "")
        {
            ShowBox.MessageBox(this.Page, "宽度不能为空!");
        }
        Nova_BLL_TBase_Position.iBackGVStyle(txtTZoneWidth.Text, txtTZoneHeight.Text, "TZoneDefine", strUserNo);
        GV_TZoneDefine_bind();
    }

    protected void ibtnTZoneDefine_Reset_Click(object sender, ImageClickEventArgs e)
    {
        txtTZoneHeight.Text = "";
        txtTZoneWidth.Text = "";
        GV_TZoneDefine_bind();
    }

    protected void ibtn_TZDefine_Submit_Click(object sender, ImageClickEventArgs e)
    {

        if (txtELcol.Text == "" || txtELine.Text == "" || txtSLcol.Text == "" || txtSLine.Text == "")
        {
            ShowBox.MessageBox(this.Page, "请选择货位号！");
            return;
        }
        else
        {
            for (int i = Convert.ToInt32(txtSLine.Text); i <= Convert.ToInt32(txtELine.Text); i++)
            {
                for (int j = Convert.ToInt32(txtSLcol.Text); j <= Convert.ToInt32(txtELcol.Text); j++)
                {
                    string strrows = GV_TZoneDefine.Rows[i - 1].Cells[j].ToolTip.ToString();
                    //PosState = Convert.ToInt32(Nova_BLL_TPostState.intPosID(strrows));

                    PosState = Convert.ToInt32(strrows);
                    //取货位状态
                    lbl_LaneNo = Convert.ToInt32(ddlNo_Lane.SelectedValue.ToString());       //巷道号
                    iLineNo = Convert.ToInt32(GV_TZoneDefine.Rows[i - 1].Cells[0].Text.Substring(0, 1));     //排
                    iLcol = j;         //列
                    iLayNo = Convert.ToInt32(GV_TZoneDefine.Rows[i - 1].Cells[1].Text.Substring(0, 1));     //层

                    Nova_MOD_TBase_Position.No_Lane = lbl_LaneNo;
                    Nova_MOD_TBase_Position.No_Line = iLineNo;
                    Nova_MOD_TBase_Position.No_Col = iLcol;
                    Nova_MOD_TBase_Position.No_Layer = iLayNo;
                    PID = Nova_BLL_TBase_Position.GetiPID(Nova_MOD_TBase_Position);
                    Nova_BLL_TZoneDefine.UpdateZoneID(PID, Convert.ToInt32(DL_TZDefine.SelectedValue.ToString()));

                    //int iflag = Nova_BLL_TZoneDefine.UpdateZoneID(PID,Convert.ToInt32(DL_TZDefine.SelectedValue.ToString()));
                    //if (iflag>0)
                    //{
                    //    ShowBox.MessageBox(this.Page, "库区设置成功");
                    //}

                }
            }
            GV_TZoneDefine_bind();

        }
    }

    protected void GV_TZDefine_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Label lblColor = (Label)e.Row.Cells[2].FindControl("lblColor");
            //lblColor.BackColor = System.Drawing.Color.FromName(lblColor.ToolTip.ToString());

            Label lblColor = (Label)e.Row.Cells[2].FindControl("lblColor");
            string strColor = "#" + lblColor.ToolTip.ToString();
            lblColor.BackColor = System.Drawing.Color.FromName(strColor);
        }
    }

    protected void DL_TZDefine_bind()
    {
        ds = Nova_BLL_TZoneDefine.DLTZoneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DL_TZDefine.DataSource = ds;
            DL_TZDefine.DataValueField = "Zone_ID";
            DL_TZDefine.DataTextField = "Zone_Name";
            DL_TZDefine.DataBind();
        }
    }

    protected void GV_TZDefine_bind()
    {
        ds = Nova_BLL_TZoneDefine.DLTZoneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            GV_TZDefine.DataSource = ds;
            GV_TZDefine.DataBind();
        }
    }

    protected bool reInt(string strNum)
    {
        try
        {
            int var1 = Convert.ToInt32(strNum);
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void ibtnSelect_Click(object sender, ImageClickEventArgs e)
    {

        if (txtSLine.Text != "" && txtSLcol.Text != "")
        {
            if (!reInt(txtSLine.Text))
            {
                ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                return;
            }
            if (!reInt(txtSLcol.Text))
            {
                ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                return;
            }
            if (txtELine.Text != "" && txtELine.Text != "")
            {
                if (!reInt(txtELine.Text))
                {
                    ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    return;
                }
                if (!reInt(txtELcol.Text))
                {
                    ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    return;
                }

                int SLine = Convert.ToInt32(txtSLine.Text.ToString());
                int ELine = Convert.ToInt32(txtELine.Text.ToString());
                int iLine = 0;

                int SLcol = Convert.ToInt32(txtSLcol.Text.ToString());
                int ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                int iLcol = 0;

                if (SLine > ELine)
                {
                    txtSLine.Text = ELine.ToString();
                    txtELine.Text = SLine.ToString();
                    iLine = SLine;
                    SLine = ELine;
                    ELine = iLine;

                }
                if (SLcol > ELcol)
                {
                    txtSLcol.Text = ELcol.ToString();
                    txtELcol.Text = SLcol.ToString();
                    iLcol = SLcol;
                    SLcol = ELcol;
                    ELcol = iLcol;
                }

                for (int x = SLine; x <= ELine; x++)
                {
                    for (int j = SLcol; j <= ELcol; j++)
                    {
                        GV_TZoneDefine.Rows[x - 1].Cells[j].BackColor = System.Drawing.Color.FromName("#ff0000");
                    }
                }
            }
            else
            {
                GV_TZoneDefine.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text)].BackColor = System.Drawing.Color.FromName("#ff0000");
            }
        }
    }
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtELcol.Text = "";
        txtELine.Text = "";
        txtSLcol.Text = "";
        txtSLine.Text = "";
        GV_TZoneDefine_bind();
    }

    protected void ddlNo_Lane_SelectedIndexChanged(object sender, EventArgs e)
    {
        GV_TZoneDefine_bind();
    }

}
